package easyway.core.exceptions;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.dao.client.DaoException;


/**
 * 
 * @author zhangqihang
 * @date Nor 23, 2009
 */
public class ExceptionHandler {

	private static final Log log = LogFactory.getLog(ExceptionHandler.class);
	
	/**
	 * 加载log4j日志配置文件
	 */
	/*static {
		PropertyConfigurator.configure(Loader.getResource("log4j.properties"));
	}*/
	

	/**
	 * 拦截所有的异常，包装后重新抛出
	 * 
	 * @param invocation
	 * @return
	 * @throws Exception
	 */
	@AroundInvoke
	public Object interceptorExceptionHandler(InvocationContext invocation)
			throws Exception {
		try {
			log.debug("进入拦截器");
			return invocation.proceed();
		} catch (DaoException e) {
			e.printStackTrace();
			log.info("DaoException");
			log.error(e.getMessage());
			throw new BusinessException("数据操作异常！");
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.info("RuntimeException");
			log.error(e.getMessage());
			throw new SystemException("系统异常！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info("Exception");
			log.error(e.getMessage());
			throw new SystemException("应用异常！");
		} finally {
			log.debug("退出拦截器");
		}
	}

}
